using System;
using System.Data;
using System.Windows.Forms;
using Company.BLL;
using Company.BLL.GC;
using Company.BLL.DuLieuChuan;
using Janus.Windows.GridEX;

namespace Company.Interface.GC
{
    public partial class PhieuChuyenTiepGCRegistedForm : BaseForm
    {
        public HopDong HopDongSelected = new HopDong();
        public bool IsBrowseForm = false;
        public PhieuChuyenTiepGCRegistedForm()
        {
            InitializeComponent();
        }

        public void BindData()
        {
            string where = string.Format("MaDoanhNghiep = '{0}'", GlobalSettings.MA_DON_VI);

        }

        //-----------------------------------------------------------------------------------------
        
        private void khoitao_DuLieuChuan()
        {
            // Đơn vị tính.
            this._DonViTinh = DonViTinh.SelectAll();

            // Đơn vị Hải quan.
            if (this.HopDongSelected.MaHaiQuan.Trim().Length == 0)
                ctrDonViHaiQuan.Ma = GlobalSettings.MA_HAI_QUAN;
            else
                ctrDonViHaiQuan.Ma = this.HopDongSelected.MaHaiQuan;
        }

        //-----------------------------------------------------------------------------------------

        private void PhieuChuyenTiepGCRegistedForm_Load(object sender, EventArgs e)
        {
            this.khoitao_DuLieuChuan();

            //lblHint.Visible = this.IsBrowseForm;
            ctrDonViHaiQuan.Enabled = !this.IsBrowseForm;
            cbLoaiPCT.DataSource = LoaiPhieuChuyenTiep.SelectAll();
            cbLoaiPCT.ValueMember = "id";
            cbLoaiPCT.DisplayMember = "Ten";
            // Hợp đồng đã đăng ký.
            //this.BindData();
            //// Doanh nghiệp / Đại lý TTHQ.
            //dgList.Tables[0].Columns["MaDoanhNghiep"].Visible = GlobalSettings.DAI_LY_TTHQ;
        }

        //-----------------------------------------------------------------------------------------
        
        private void btnGetListFromHQ_Click(object sender, EventArgs e)
        {
            //this.dsRegistedList = new HopDong().WS_GetDanhSachDaDangKy(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI);
            //dgList.DataSource = this.dsRegistedList.Tables[0];
            //// Cập nhật vào CSDL.
            //bool ret = new HopDong().UpdateRegistedToDatabase(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI, this.dsRegistedList);
            //bool retNPL = new NguyenPhuLieu().UpdateRegistedToDatabase(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI);
            //bool retSP = new SanPham().UpdateRegistedToDatabase(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI);
            //bool retPK = new PhuKien().UpdateRegistedToDatabase(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI);
            
            //if (ret && retNPL & retSP & retPK)
            //{
            //    MessageBox.Show("Cập nhật thành công");
            //}

        }

        //-----------------------------------------------------------------------------------------
        
        private void dgList_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            //if (e.Row.RowType == RowType.Record)
            //{
            //    string soHD = e.Row.Cells["SoHopDong"].Value.ToString();
            //    string maHQ = e.Row.Cells["MaHaiQuan"].Value.ToString();
            //    string maDN = e.Row.Cells["MaDoanhNghiep"].Value.ToString();
            //    DateTime ngayKy = Convert.ToDateTime(e.Row.Cells["NgayKy"].Value);
            //    DateTime ngayHH = Convert.ToDateTime(e.Row.Cells["NgayHetHan"].Value);
            //    this.HopDongSelected.SoHopDong = soHD;
            //    this.HopDongSelected.MaHaiQuan = maHQ;
            //    this.HopDongSelected.MaDoanhNghiep = maDN;
            //    this.HopDongSelected.NgayKy = ngayKy;
            //    this.HopDongSelected.NgayHetHan = ngayHH;
                
            //    if (this.IsBrowseForm)
            //    {                
            //        this.Close();
            //    }
            //    else
            //    {
            //        HopDongRegistedDetailForm f = new HopDongRegistedDetailForm();
            //        f.HD = this.HopDongSelected;
            //        f.ShowDialog();
            //    }                    
            //}
        }

        private void ctrDonViHaiQuan_ValueChanged(object sender, EventArgs e)
        {
            this.BindData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                if (e.Row.Cells["NgayGiaHan"].Value != DBNull.Value)
                {
                    if (Convert.ToDateTime(e.Row.Cells["NgayGiaHan"].Value).Year == 1900)
                    {
                        e.Row.Cells["NgayGiaHan"].Text = "";
                    }
                }
            }
        }

        //-----------------------------------------------------------------------------------------
    }
}